#!/usr/bin/env python
"""This module defines our game-specific errors. It also includes a list 
fault_codes, which is a mapping between XML-RPC Fault and an actual exception.

This list is valid for python 2.6, in other versions, there may be other builtin
exceptions. The XMLRPC server returns -1 for unknown exceptions (whose name is
still available as string in the Fault. Moreover, the server exposes the
error_list() method which returns exception names prefixed by module name.

The client can import error, catch xml-rpc faults and then 
raise error.fault_codes[fault_code](constructor_args)
"""

__version__ = "$Revision: 14 $"

class FieldOccupied(Exception):
    pass
    
class GameFull(Exception):
    max_players = None
    def __init__(self, max_players=None):
        message = "The game is full."
        if max_players is not None:
            message += " The max # of players is %s" % max_players
        Exception.__init__(self, message)
        self.max_players = max_players

class WrongSecurityDigest(Exception):
    pass

class NotYourTurn(Exception):
    pass


# Add exceptions TO THE END to assure compatibility if client and server is not
# running the same version.
fault_codes =  [# First the builtins
                ArithmeticError ,          
                AssertionError ,           
                AttributeError ,           
                BufferError ,              
                BytesWarning ,
                DeprecationWarning ,
                EOFError ,
                EnvironmentError ,
                Exception ,
                FloatingPointError ,
                FutureWarning ,
                IOError ,
                ImportError ,
                ImportWarning ,
                IndentationError ,
                IndexError ,
                KeyError ,
                LookupError ,
                MemoryError ,
                NameError ,
                NotImplementedError ,
                OSError ,
                OverflowError ,
                PendingDeprecationWarning ,
                ReferenceError ,
                RuntimeError ,
                RuntimeWarning ,
                StandardError ,
                StopIteration ,
                SyntaxError ,
                SyntaxWarning ,
                SystemError ,
                TabError ,
                TypeError ,
                UnboundLocalError ,
                UnicodeDecodeError ,
                UnicodeEncodeError ,
                UnicodeError ,
                UnicodeTranslateError ,
                UnicodeWarning ,
                UserWarning ,
                ValueError ,
                Warning ,
                ZeroDivisionError,
                # Then locally defined exceptions
                FieldOccupied ,
                GameFull ,     
                NotYourTurn,
                WrongSecurityDigest
                # And finally any new exceptions (introduced in new python or 
                # new version of this program). In the first production release,
                # there must be no item below this comment and this sentece must disappear.
                ]

if __name__ == "__main__":
    import random
    random = random.randrange(0, len(fault_codes))
    print random
    raise fault_codes[random]

